package twidledee.client;

import java.util.ArrayList;

import twitter4j.Status;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Twidledee implements EntryPoint {

	private HorizontalPanel mainPanel = new HorizontalPanel();
	private VerticalPanel tweetPanel = new VerticalPanel();
	private VerticalPanel settingsPanel = new VerticalPanel();
	private FlexTable tweetsFlexTable = new FlexTable();
	private Label viewDescriptionLabel = new Label();
	
	//The widgets for the right "settings" panel
	private MenuBar mainMenu = new MenuBar();
	private TextBox searchBox = new TextBox();
	private Button searchButton = new Button("search");
	private StackPanel tweetBrowser = new StackPanel();
	private Tree categoriesTree = new Tree();
	private StackPanel favourites = new StackPanel();
	private StackPanel saved = new StackPanel();
	private StackPanel trends = new StackPanel();

	private ArrayList<String> tweets = new ArrayList<String>();
	private ServicesRegistry servicesRegistry = new ServicesRegistry();

	/**
	 * Entry point method.
	 */
	public void onModuleLoad() {
		viewDescriptionLabel.setText("Latest Tweets in your area...");
		
		/*
		addTweet("http://images.ourfaves.com/99/090_1.jpg",
				"Aldo@CollegeSq", "20% of All Sandals at our College Square location. All day today!!!", "shoes");
		addTweet("http://www.yassfc.com.au/images/mcdonalds_logo.jpg",
				"McDonald's", "OMG free McGridle's! This morning only, Bank&Strandherd", "food");
		addTweet("http://s.twimg.com/a/1250203207/images/default_profile_bigger.png",
				"Pub101", "2.50 jaggerbombs tonight only", "nightlife");
		addTweet("http://www.twinsdays.org/images/sponsor_logos/BestBuyLogo.jpg",
				"BestBuy@SouthKeys", "sale on right now!", "electronics");
		addTweet("http://images.ctv.ca/archives/CTVNews/img2/20080808/160_bc_running_room_logo_080808.jpg",
				"RunningRoom@Downtown", "deals on shoes! Nike for $49.99", "shoes");
		*/

		// Assemble Main panel.
		mainPanel.add(tweetPanel);
		mainPanel.add(settingsPanel);

		// Assemble Tweet panel.
		tweetPanel.add(viewDescriptionLabel);
		tweetPanel.add(tweetsFlexTable);
		
		//Assemble Menu
		Command menuCommand = new Command(){
			public void execute(){
				
			}
		};
		mainMenu.addItem("home", menuCommand);
		mainMenu.addItem("settings", menuCommand);
		mainMenu.addItem("help", menuCommand);
		settingsPanel.add(mainMenu);
		
		//Assemble Search box
		HorizontalPanel searchPanel = new HorizontalPanel();
		searchPanel.add(searchBox);
		searchPanel.add(searchButton);
		settingsPanel.add(searchPanel);
		
		//Assemble the Categories panel
		String[] clothingCategories = {"shoes", "kids", "mens", "womens"};
		TreeItem clothing = categoriesTree.addItem("Clothing");
		populateCategory(clothing, clothingCategories);
		String[] foodCategories = {"fast", "fine", "grocery", "dessert"};
		TreeItem food = categoriesTree.addItem("Food");
		populateCategory(food, foodCategories);
		String[] sportsCategories = {"Hockey", "Soccer", "Skiing", "Snowboarding", "Cycling"};
		TreeItem sports = categoriesTree.addItem("Sports");
		populateCategory(sports, sportsCategories);
		String[] entertainmentCategories = {"Nightlife", "Theatre&Cinema"};
		TreeItem entertainment = categoriesTree.addItem("Entertainment");
		populateCategory(entertainment, entertainmentCategories);
		String[] electronicsCategories = {"Computers", "Phones", "TVs", "Accessories"};
		TreeItem electronics = categoriesTree.addItem("Electronics");
		populateCategory(electronics, electronicsCategories);
		String[] eventsCategories = {"Events"};
		TreeItem events = categoriesTree.addItem("Events");
		populateCategory(events, eventsCategories);
		tweetBrowser.add(categoriesTree, "Categories");
		
		//Assemble the Favourites panel
		tweetBrowser.add(favourites, "Favourites");
		
		//Assemble the SavedTweets panel
		tweetBrowser.add(saved, "Saved");
		
		//Assemble the Trends panel
		tweetBrowser.add(trends, "Trends");

		
		settingsPanel.add(tweetBrowser);
		// Associate the Main panel with the HTML host page.
		RootPanel.get("mainPanel").add(mainPanel);
		
		registerUser("twdeetest", "123456789" , "246 Queen Street", "K5B 07J");
		getTweets();
	}
	
	private void addTweet(String imageUrl, String userName, String tweet, String category){
		Image image = new Image(imageUrl);
		image.setSize("48px", "48px");
		int row = tweets.size();
		Button btnCategory = new Button("cat");
		btnCategory.setTitle(category);
		Button btnMap = new Button("map");
		btnMap.setTitle("Where is this deal?");
		Button btnLike = new Button("like");
		btnLike.setTitle("I like this!");
		Button btnComment = new Button("cmt");
		btnComment.setTitle("I'd like to comment on this");
		
		tweetsFlexTable.setWidget(row, 0, image);
		tweetsFlexTable.setText(row, 1, userName);
		tweetsFlexTable.setText(row, 2, tweet);
		tweetsFlexTable.setWidget(row, 3, btnCategory);
		tweetsFlexTable.setWidget(row, 4, btnMap);
		tweetsFlexTable.setWidget(row, 5, btnLike);
		tweetsFlexTable.setWidget(row, 6, btnComment);
		
		tweets.add(tweet);
	}
	
	private void populateCategory(TreeItem item, String[] subCategories){
		for (String category : subCategories){
			item.addItem(category);
		}
	}
	
	private void serviceTest(){
		if (servicesRegistry.twidledeeService == null) servicesRegistry.twidledeeService = GWT.create(TwidledeeService.class);
		
		AsyncCallback<String> callback = new AsyncCallback<String>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("call failed");
				
			}
			@Override
			public void onSuccess(String result) {
				Window.alert(result);
				
			}
		}; 
		servicesRegistry.twidledeeService.testService("hello server", callback);
	}
	
	private void registerUser(String userName, String password, String address, String postalCode){
		if (servicesRegistry.registerUserService == null) servicesRegistry.registerUserService = GWT.create(RegisterUserService.class);
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onSuccess(Boolean result) {
				if (result) Window.alert("registration successfull");
				else Window.alert("Your registration could not be completed");
			}
		};
		servicesRegistry.registerUserService.registerUser(new User(userName, password, address, postalCode), callback);
	}
	
	private void getTweets(){
		if (servicesRegistry.tweetUpdateService == null) servicesRegistry.tweetUpdateService = GWT.create(TweetUpdateService.class);
		
		AsyncCallback<Tweet[]> callback = new AsyncCallback<Tweet[]>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Tweet[] result) {
				updateCurrentTweets(result);
			}
			
		};
	}
	
	private void updateCurrentTweets(Tweet[] tweets){
		for (int i=0; i<tweets.length; i++){
			addTweet("", tweets[i].getUser().getUserName(), tweets[i].getText(), "unspecified");
		}
	}

}
